package com.artieksi.action;

import com.artieksi.common.GuiMessage;
import com.artieksi.dao.ArtiEksiDAO;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import twitter4j.Twitter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Map;

/**
 * User: TTACANSEVER
 */
@Component
public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware, CookiesAware {

    @Autowired
    protected ArtiEksiDAO artiEksiDAO;

    @Autowired
    protected Twitter twitter;

    @Autowired
    protected MessageSource messageSource;

    protected Map<String, Object> session;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected Map<String, String> cookiesMap;

    protected String invokeMethod;
    protected String tableList;
    protected String tableList2;
    protected String tableList3;
    protected String tableList4;
    protected InputStream guiMessageStream;
    protected InputStream exportStream;
    protected String exportDAO;
    protected String exportSearchProperty;
    protected int wantedPage;
    protected String queryStr;

    public static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
    public HttpServletRequest getServletRequest() {
        return request;
    }
    @Override
    public void setServletRequest(HttpServletRequest req){
        this.request = req;
    }
    public HttpServletResponse getServletResponse() {
        return response;
    }

    @Override
    public void setServletResponse(HttpServletResponse resp) {
        this.response = resp;
    }

    public void addToSession(String key, Object o) {
        session.put(key, o);
    }

    public Object getFromSession(String key) {
        return session.get(key);
    }

    public Object getFromRequest(String key) {
        return request.getParameter(key);
    }

    public String getMessage(String key) {
        return messageSource.getMessage(key, null, new Locale("tr", "TR"));
    }

    public String getMessage(String key, Object[] params) {
        return messageSource.getMessage(key, params, new Locale("tr", "TR"));
    }

    @Override
    public void setCookiesMap(Map<String, String> stringStringMap) {
        cookiesMap = stringStringMap;
    }

    public String getInvokeMethod() {
        return invokeMethod;
    }

    public void setInvokeMethod(String invokeMethod) {
        this.invokeMethod = invokeMethod;
    }

    public String getTableList() {
        return tableList;
    }

    public void setTableList(String tableList) {
        this.tableList = tableList;
    }

    public String getTableList2() {
        return tableList2;
    }

    public void setTableList2(String tableList2) {
        this.tableList2 = tableList2;
    }

    public String getTableList3() {
        return tableList3;
    }

    public void setTableList3(String tableList3) {
        this.tableList3 = tableList3;
    }

    public String getTableList4() {
        return tableList4;
    }

    public void setTableList4(String tableList4) {
        this.tableList4 = tableList4;
    }

    public InputStream getGuiMessageStream() {
        return guiMessageStream;
    }

    public void setGuiMessageStream(InputStream guiMessageStream) {
        this.guiMessageStream = guiMessageStream;
    }

    public int getWantedPage() {
        return wantedPage;
    }

    public void setWantedPage(int wantedPage) {
        this.wantedPage = wantedPage;
    }

    public String getQueryStr() {
        return queryStr;
    }

    public void setQueryStr(String queryStr) {
          this.queryStr = queryStr;
    }

    public int[] getNumbers(int start, int end) {
        int numbers[] = new int[end - start + 1];
        int index = 0;
        for (int i = start; i <= end; i++) {
            numbers[index] = i;
            index++;
        }
        return numbers;
    }

    public ByteArrayInputStream getMessageStream(String severity, String message) throws IOException {
        return new ByteArrayInputStream(new ObjectMapper().writeValueAsString(new GuiMessage(severity, message)).getBytes());
    }

    public ByteArrayInputStream getResponseStream(Object o) throws IOException {
        return new ByteArrayInputStream(new ObjectMapper().writeValueAsString(o).getBytes());
    }

    public InputStream getExportStream() {
        return exportStream;
    }

    public void setExportStream(InputStream exportStream) {
        this.exportStream = exportStream;
    }

    public String getExportDAO() {
        return exportDAO;
    }

    public void setExportDAO(String exportDAO) {
        this.exportDAO = exportDAO;
    }

    public String getJsonString(Object o) throws IOException {
        return new ObjectMapper().writeValueAsString(o);
    }

    public String getExportSearchProperty() {
        return exportSearchProperty;
    }

    public void setExportSearchProperty(String exportSearchProperty) {
        this.exportSearchProperty = exportSearchProperty;
    }

    public ObjectMapper getMapper(){
        return  new ObjectMapper();
    }

    public ObjectMapper getMapperWithDateFormat(String format){
        ObjectMapper mapper = new ObjectMapper();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        mapper.getDeserializationConfig().setDateFormat(sdf);
        return mapper;
    }


}
